/*
 * Trace
 *
 * SJSU CS 149 Spring 2009 Project
 *
 * Copyright 2009 Timmy Yee
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.JTextArea;
import java.awt.Font;
import javax.swing.SwingUtilities;

/**
 * The Trace class has a text area box that holds the trace of a simulation
 * run.
 *
 * @author Timmy Yee
 */
public final class Trace
{
    private static JTextArea trace_;

    /**
     * A private constructor to prevent instantiating this static class.
     */
    private Trace() {}

    /**
     * Creates a text area for the user interface.
     *
     * @param font Font for the text inside the text area.
     */
    public static void initialize(Font font)
    {
        trace_ = new JTextArea(20, 80);
        trace_.setEditable(false);
        trace_.setFont(font);
    }

    /**
     * Gets the trace text area.
     *
     * @return Text area for the user interface.
     */
    public static JTextArea getTextArea()
    {
        return trace_;
    }

    /**
     * Gets rid of any text displayed in the trace text area.
     */
    public static void clear()
    {
        trace_.setText(null);
    }

    /**
     * Appends the trace without any additional formatting.
     *
     * @param raw   The string to append.
     * @param objs  The objects that are to be passed through the formatter.
     */
    public static void append(final String raw, final Object ... objs)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                trace_.append(String.format(raw, objs));
            }
        });
    }

    /**
     * Appends the trace with the formatted current time.
     *
     * @param fmt   A format string containing an event to log.
     * @param objs  The objects that are to be passed through the formatter.
     */
    public static void log(String fmt, Object ... objs)
    {
        append("%.1f\t\t%s\n",
               Simulator.getCurrentTime(), String.format(fmt, objs));
    }
}

